package com.itlong.cloud.controller.face;

import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.operate.OperateFaceApplyInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateFaceApplyInfoGetPageDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateUserFaceInfoPageDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.OperateFaceApplyInfoVO;
import com.itlong.cloud.POJO.VO.operate.OperateUserFaceInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.OperateErrorCodeEnum;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.project.operate.IOperateFaceApplyServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.netflix.discovery.converters.Auto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      运营-人脸识别-应用管理。
 * <desc/>
 *
 * @createDate 2018-04-24.
 */
@RestController
@RequestMapping("/face/applyManage")
public class OperateApplyManageController {


    @Autowired
    IOperateFaceApplyServiceClient iOperateFaceApplyServiceClient;
    /**
     * <desc>
     *      添加百度人脸库信息。
     * <desc/>
     *
     * @param  saveDTO 添加人脸库保存参数
     * @return
     * @author shengen.T
     * @createDate 2018-04-24
     */
    @AuthAccessRequired
    @RequestMapping("/save")
    @LoggerInfo(operateDesc = LogDescConstant.APPLY_FACE_SAVE)
    public Object saveFaceInfo(OperateFaceApplyInfoDTO saveDTO){
        MessageVO messageVO;
        try{
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            saveDTO= HttpProtocolUtil.parseRequestParamToDTO(OperateFaceApplyInfoDTO.class,request);
            //判断对象
            if(null == saveDTO){
                return new MessageVO(BaseErrorCodeEnum.PARAM_ERR.getErrorCode());
            }
            //判断应用名称
            if(StringUtils.isBlank(saveDTO.getApplyName())){
                return new MessageVO(OperateErrorCodeEnum.FACE_APPLY_NAME_EMPTY_ERR.getErrorCode());
            }
            //判断appId
            if(StringUtils.isBlank(saveDTO.getAppId()) || !saveDTO.getAppId().matches("[0-9]{8}")){
               return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_INVALID_ERR.getErrorCode());
            }
            //判断apiKey
            if(StringUtils.isBlank(saveDTO.getApiKey())){
                return new MessageVO(OperateErrorCodeEnum.FACE_API_KEY_EMPTY_ERR.getErrorCode());
            }
            //判断secretkey
            if(StringUtils.isBlank(saveDTO.getSecretKey())){
                return new MessageVO(OperateErrorCodeEnum.FACE_SECRET_KEY_EMPTY_ERR.getErrorCode());
            }
            Map<String,Object> result  = iOperateFaceApplyServiceClient.saveFaceApplyInfo(saveDTO);
            if("1".equals(result.get("code").toString())){
                 return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_EXIST_ERR.getErrorCode());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_人脸管理_应用管理add】添加人脸应用信息失败",e);
        }
        return messageVO;
    }

     /**
      * <desc>
      *      更新人脸应用信息。
      * <desc/>
      *
      * @param  applyName 应用名称
      * @param  appId 编号
      * @param  remark 备注
      * @return
      * @author shengen.T
      * @createDate 2018-05-04
      */
    @AuthAccessRequired
    @RequestMapping("/update")
    @LoggerInfo(operateDesc = LogDescConstant.APPLY_FACE_UPDATE)
    public Object updateFaceApplyInfo(String applyName,String remark,String appId, Integer id){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            applyName=request.getAttribute("applyName")==null?null:request.getAttribute("applyName").toString();
            remark=request.getAttribute("remark")==null?"":request.getAttribute("remark").toString();
            appId=request.getAttribute("appId")==null?null:request.getAttribute("appId").toString();
            id=request.getAttribute("id")==null?null:Integer.parseInt(request.getAttribute("id").toString());
            //判断appId
            if(StringUtils.isBlank(appId) || !appId.matches("[0-9]{8}")){
                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_INVALID_ERR.getErrorCode());
            }
            //判断应用名称
            if(StringUtils.isBlank(applyName)){
                return new MessageVO(OperateErrorCodeEnum.FACE_APPLY_NAME_EMPTY_ERR.getErrorCode());
            }
            //更新应用信息
            iOperateFaceApplyServiceClient.updateFaceApplyInfo(applyName,remark,appId,id);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_人脸管理_应用管理update】更新人脸应用信息失败",e);
        }
        return messageVO;
    }

//     /**
//      * <desc>
//      *      检测该APPId的应用是否被使用了，绑定了项目就不允许删除了。
//      * <desc/>
//      *
//      * @param appId
//      * @return
//      * @author shengen.T
//      * @createDate 2018-05-04
//      */
//    @RequestMapping("/checkAppIdIsUse")
//    public Object checkAppIdIsUse(String appId){
//        MessageVO messageVO;
//        try {
//            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//            appId=request.getAttribute("appId")==null?null:request.getAttribute("appId").toString();
//            //判断appId
//            if(StringUtils.isBlank(appId) || !appId.matches("[0-9]{8}")){
//                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_INVALID_ERR.getErrorCode());
//            }
//            Integer res = iOperateFaceApplyServiceClient.checkAppIdIsUse(appId);
//            if(res>0){
//                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_USE_ERR.getErrorCode());
//            }
//            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
//        }catch (Exception e){
//            throw new DataAccessException("【运营_人脸管理_应用管理check】检测该APPId的应用是否被使用失败",e);
//        }
//        return messageVO;
//    }

     /**
      * <desc>
      *      删除人脸应用信息。
      * <desc/>
      *
      * @param  appId
      * @return
      * @author shengen.T
      * @createDate 2018-05-04
      */
     @AuthAccessRequired
     @RequestMapping("/delete")
     @LoggerInfo(operateDesc = LogDescConstant.APPLY_FACE_DELETE)
    public Object deleteFaceApplyInfo(String appId,Integer id){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appId=request.getAttribute("appId")==null?null:request.getAttribute("appId").toString();
            id=request.getAttribute("id")==null?null:Integer.parseInt(request.getAttribute("id").toString());
            //判断appId
            if(StringUtils.isBlank(appId)){
                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_INVALID_ERR.getErrorCode());
            }
            Integer isBing = iOperateFaceApplyServiceClient.checkAppIdIsUse(appId);
            if(isBing>0){
                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_USE_ERR.getErrorCode());
            }
            iOperateFaceApplyServiceClient.deleteFaceApplyInfo(appId,id);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【运营_人脸管理_应用管理delete】删除人脸应用信息失败",e);
        }
        return messageVO;
    }


     /**
      * <desc>
      *      获取单个人脸应用信息。
      * <desc/>
      *
      * @param  appId 百度人脸库唯一Id
      * @return
      * @author shengen.T
      * @createDate 2018-05-04
      */
    @RequestMapping("/getOne")
    public Object getOneFaceApplyInfo(String appId, Integer id){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            appId=request.getAttribute("appId")==null?null:request.getAttribute("appId").toString();
            id=request.getAttribute("id")==null?null:Integer.parseInt(request.getAttribute("id").toString());
            //判断appId
            if(StringUtils.isBlank(appId) || !appId.matches("[0-9]{8}")){
                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_INVALID_ERR.getErrorCode());
            }
            OperateFaceApplyInfoVO infoVO = iOperateFaceApplyServiceClient.getOneInfo(appId,id);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),infoVO);
        }catch (Exception e){
            throw new DataAccessException("【运营_人脸管理_应用管理getOne】获取单个人脸应用信息失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      分页获取人脸应用信息。
     * <desc/>
     *
     * @param  pageDTO 分页查询DTO
     * @return
     * @author shengen.T
     * @createDate 2018-05-07
     */
    @AuthAccessRequired
    @RequestMapping("/getPage")
    public Object getPageFaceApplyInfo(OperateFaceApplyInfoGetPageDTO pageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO=HttpProtocolUtil.parseRequestParamToDTO(OperateFaceApplyInfoGetPageDTO.class,request);
            Page<OperateFaceApplyInfoVO> resultPage = iOperateFaceApplyServiceClient.getPageInfo(pageDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),resultPage);
        }catch (Exception e){
            throw new DataAccessException("【运营_人脸管理_应用管理getPage】分页获取人脸应用信息失败",e);
        }
        return messageVO;
    }

     /**
      * <desc>
      *      分页获取人脸库信息。
      * <desc/>
      *
      * @param  pageDTO 分页人脸库查询对象
      * @return
      * @author shengen.T
      * @createDate 2018-05-07
      */
     @AuthAccessRequired
     @RequestMapping("/getFacePage")
    public Object getFacePage(OperateUserFaceInfoPageDTO pageDTO){
        MessageVO messageVO;
        try {
            HttpServletRequest request=((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            pageDTO=HttpProtocolUtil.parseRequestParamToDTO(OperateUserFaceInfoPageDTO.class,request);
            if(null == pageDTO){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //判断appId
            if(StringUtils.isBlank(pageDTO.getAppId()) || !pageDTO.getAppId().matches("[0-9]{8}")){
                return new MessageVO(OperateErrorCodeEnum.FACE_APP_ID_INVALID_ERR.getErrorCode());
            }
            Page<OperateUserFaceInfoVO> resultVO = iOperateFaceApplyServiceClient.getFacePage(pageDTO);
            OperateFaceApplyInfoVO infoVO = iOperateFaceApplyServiceClient.getOneInfo(pageDTO.getAppId(),pageDTO.getId());
            JSONObject js = new JSONObject();
            js.put("faceData",resultVO);
            if((null!=infoVO)&&(StringUtils.isNotBlank(infoVO.getApplyName()))){
                js.put("applyName",infoVO.getApplyName());
            }
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),js);
        }catch (Exception e){
            throw new DataAccessException("【运营_人脸管理_应用管理getFacePage】分页获取人脸库信息失败",e);
        }
        return messageVO;
    }

}
